package com.epay;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
public class MysqlConnect{
	public boolean connect(){
		 Connection conn = null;
		 PreparedStatement pstmt = null;
		ResultSet rs = null;
	        try {
	            Class.forName(Constants.dbDriver);// 动态加载mysql驱动
	 
	            System.out.println("成功加载MySQL驱动程序");
	            // 一个Connection代表一个数据库连接
	            conn = DriverManager.getConnection(Constants.dbURL,Constants.dbUser,Constants.dbPassword);
	            // Statement里面带有很多方法，比如executeUpdate可以实现插入，更新和删除等

				System.out.println("SELECT column_name,column_comment,data_type FROM INFORMATION_SCHEMA.Columns "+
						" where table_name='"+Constants.tableName.toUpperCase()+"' AND table_schema='"+Constants.dbName+"' ORDER BY ORDINAL_POSITION");
	            pstmt=conn.prepareStatement("SELECT column_name,column_comment,data_type FROM INFORMATION_SCHEMA.Columns "+
	       			 " where table_name='"+Constants.tableName.toUpperCase()+"' AND table_schema='"+Constants.dbName+"' ORDER BY ORDINAL_POSITION");
	            rs = pstmt.executeQuery();
	            //创建bean
	            CreaterBean.createrBean(rs);
	            CreaterMapper.createrMapper(Constants.objMap);
	            CreaterDao.createrDao(Constants.objMap);
	            CreaterService.createrService();
	            CreaterController.createrController();
	            CreaterDto.createrDto(Constants.objMap);
	        } catch (SQLException e) {
	            System.out.println("MySQL操作错误");
	            e.printStackTrace();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        return true;
	}

	
    public static void main(String[] args) throws Exception {
         try {

        	 Constants.init();
        	 MysqlConnect connect=new MysqlConnect();
        	 connect.connect();
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
 
}